﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;

namespace MessageRouter.ServiceBroker.Configuration
{
    internal sealed class ServiceBrokerBehaviorConfigurationElement : ConfigurationElement
    {
        const string namePropertyName = "name";
        const string activatorPropertyName = "activator";
        const string dialogLifetimePropertyName = "dialogLifetimePolicy";

        private static readonly ConfigurationProperty nameProperty = new ConfigurationProperty(namePropertyName, typeof(string), null, ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty activatorProperty = new ConfigurationProperty(activatorPropertyName, typeof(Type), null, new TypeNameConverter(), null, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty dialogLifetimeProperty = new ConfigurationProperty(dialogLifetimePropertyName, typeof(DialogLifetimePolicy), DialogLifetimePolicy.CloseAfterReceive);
        private static readonly ConfigurationPropertyCollection properties;

        [ConfigurationProperty(namePropertyName, IsKey = true, IsRequired = true)]
        internal string Name
        {
            get { return (string)base[nameProperty]; }
        }

        [TypeConverter(typeof(TypeNameConverter))]
        [ConfigurationProperty(activatorPropertyName, IsKey = false, IsRequired = true)]
        internal Type Activator
        {
            get { return (Type)base[activatorProperty]; }
        }

        [ConfigurationProperty(dialogLifetimePropertyName, IsKey = true, DefaultValue = DialogLifetimePolicy.CloseAfterReceive)]
        internal DialogLifetimePolicy DialogLifetimePolicy
        {
            get { return (DialogLifetimePolicy)base[dialogLifetimeProperty]; }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get { return properties; }
        }

        internal ServiceBrokerBehaviorConfigurationElement() { }

        static ServiceBrokerBehaviorConfigurationElement()
        {
            properties = new ConfigurationPropertyCollection
            {
                nameProperty,
                activatorProperty,
                dialogLifetimeProperty,
            };
        }
    }
}
